package cn.xfdzx.android.apps.shop.presenter;

import cn.xfdzx.android.apps.shop.base.BasePresenter;
import cn.xfdzx.android.apps.shop.bean.BaseObjectBean;
import cn.xfdzx.android.apps.shop.bean.ConfirmGoodBeen;
import cn.xfdzx.android.apps.shop.bean.ShopCarListBeen;
import cn.xfdzx.android.apps.shop.contract.ShopCarListContract;
import cn.xfdzx.android.apps.shop.model.ShopCarListModel;
import cn.xfdzx.android.apps.shop.net.RxScheduler;
import io.reactivex.functions.Consumer;

/**
 * @author
 * @date Description：
 */
public class ShopCarListPresenter extends BasePresenter<ShopCarListContract.View> implements ShopCarListContract.Presenter {

    private ShopCarListContract.Model model;

    public ShopCarListPresenter() {
        model = new ShopCarListModel();
    }


    @Override
    public void initData(String type) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.initData(  type)
                .compose(RxScheduler.<ShopCarListBeen>Flo_io_main())
                .as(mView.<ShopCarListBeen>bindAutoDispose())
                .subscribe(new Consumer<ShopCarListBeen>() {
                    @Override
                    public void accept(ShopCarListBeen bean) throws Exception {
                        mView.onInitSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void updateCarNum(String id,int number,int one_position,int position) {
        if (!isViewAttached()) {
            return;
        }
//        mView.showLoading();
        model.updateCarNum(id,number+"")
                .compose(RxScheduler.<BaseObjectBean>Flo_io_main())
                .as(mView.<BaseObjectBean>bindAutoDispose())
                .subscribe(new Consumer<BaseObjectBean>() {
                    @Override
                    public void accept(BaseObjectBean bean) throws Exception {
                        mView.onUpdateCarNumSuccess(bean,number,one_position,position);
//                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
//                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void removeGood(String ids,int one_position,int position,boolean isAll) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.removeGood(ids)
                .compose(RxScheduler.<BaseObjectBean>Flo_io_main())
                .as(mView.<BaseObjectBean>bindAutoDispose())
                .subscribe(new Consumer<BaseObjectBean>() {
                    @Override
                    public void accept(BaseObjectBean bean) throws Exception {
                        mView.onRemoveGoodSuccess(bean,one_position,position,isAll);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void confirm(String  cartId,String  user_curent_lng,String  user_curent_lat) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.confirm(cartId,user_curent_lng,user_curent_lat)
                .compose(RxScheduler.<ConfirmGoodBeen>Flo_io_main())
                .as(mView.<ConfirmGoodBeen>bindAutoDispose())
                .subscribe(new Consumer<ConfirmGoodBeen>() {
                    @Override
                    public void accept(ConfirmGoodBeen bean) throws Exception {
                        mView.onConfirmSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void setNum(int one_position, int two_position) {
        mView.dialog_num(one_position,two_position);
    }


}
